/*
 * Yet Another Haxe Library, Copyright (c) 2009, Jean-Philippe CIMAN (Jeanffy)
 * All rights reserved.
 * Permission is hereby granted to use, copy, modify, and distribute this
 * source code, or portions hereof, for any purpose, without fee, subject
 * to the following restrictions:
 *
 * 1. The origin of this source code must not be misrepresented.
 * 2. Altered versions must be plainly marked as such and
 *    must not be misrepresented as being the original source.
 * 3. This copyright notice may not be removed or altered from
 *    any source or altered source distribution.
 *
 * If you use this source code in a product, acknowledgment is not
 * required but would be appreciated.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE PROJECT AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
import flash.MovieClip;

/**
	This class handles creation of sprites and depths<br/>
	A clip receives the created sprites<br/>
	Depths varies between a min and a max. Once the max is reached, the depth loops to the min value.<br/>
	Example : if min = 10 and max = 13, the class will create sprites with depths 10, 11, 12, 13, 10, 11, ...<br/>
	Warning : when depth is looping to min, the previously created sprite with depth min will be replaced<br/>
	As the class is static, there can be only one manager for each project, so only one movieclip is handled by the manager
**/
class YahSpriteManager
{
	private static var m_clip:MovieClip; // the movieclip that will receive created sprites
	private static var m_minDepth:Int; // min depth
	private static var m_maxDepth:Int; // max depth
	private static var m_nextDepth:Int; // next available depth
	
	public function new()
	{
		/** by defaut created movieclips will be on _root, here we create depths from 0 to 100 **/
		initialize(0, 100, flash.Lib.current);
	}
	
	/**
		Initializes the manager - to be called before anything<br/>
		clip (opt) : movieclip that will receive created sprites. If not provided, created sprites will be on flash.Lib.current (_root)
	**/
	public static function initialize(minDepth:Int, maxDepth:Int, ?clip:MovieClip)
	{
		if (clip != null)
			m_clip = clip;
		m_minDepth = minDepth;
		m_maxDepth = maxDepth;
		m_nextDepth = m_minDepth;
	}
	
	/**
		Attaches a movieclip
		linkage : linkage name of movieclip in library
		posx, posy (opt) : initial position
		depth (opt) : imposed depth
	**/
	public static function attach(linkage:String, ?posx:Float, ?posy:Float, ?depth:Int) : MovieClip
	{
		var dep:Int = depth, thex:Float = posx, they:Float = posy;
		if (dep == null)
			dep = getNextDepth();
		if (thex == null)
			thex = 0;
		if (they == null)
			they = 0;
		var spr:MovieClip = m_clip.attachMovie(linkage, linkage+dep, dep, {_x:thex, _y:they});
		spr.cacheAsBitmap = true; // TODO see if really needed, or make it an option
		return spr;
	}
	
	/**
		Creates an empty movieclip
		name : name that will be given to the created movieclip
		posx, posy (opt) : initial position
		depth (opt) : imposed depth
	**/
	public static function create(name:String, ?posx:Float, ?posy:Float, ?depth:Int) : MovieClip
	{
		var dep:Int = depth, thex:Float = posx, they:Float = posy;
		if (dep == null)
			dep = getNextDepth();
		if (thex == null)
			thex = 0;
		if (they == null)
			they = 0;
		var spr:MovieClip = m_clip.createEmptyMovieClip(name+dep, dep);
		spr._x = thex;
		spr._y = they;
		spr.cacheAsBitmap = true; // TODO see if really needed, or make it an option
		return spr;
	}
	
	/**
		Returns the next available depth to attach a movieclip and increments it
	**/
	private static function getNextDepth() : Int
	{
		if (m_nextDepth > m_maxDepth)
			m_nextDepth = m_minDepth;
		m_nextDepth++;
		return m_nextDepth - 1;
	}
}
